package cn.framelife.rainbow.db.creator.part;

import cn.framelife.rainbow.db.creator.CodeCreator;
import cn.framelife.rainbow.db.entity.ClazzProperty;
import cn.framelife.rainbow.db.entity.PanelProperty;
import cn.framelife.rainbow.db.entity.Property;
import cn.framelife.rainbow.ui.entity.Module;
import cn.framelife.rainbow.util.FreemarkerUtil;
import cn.framelife.rainbow.util.StringUtil;

/**
 * 页面生成对象
 * 
 * @author 潘广伟 p_3er@qq.com  
 * @date 2014-3-10 下午6:46:58
 */
public class PageCreator {
	private EntityCreator entityCreator;

	public PageCreator(EntityCreator entityCreator) {
		this.entityCreator = entityCreator;
	}

	public void createCodeForPage(Module module,ClazzProperty entity) throws Exception {
		FreemarkerUtil.createPanel(createPanel(module,entity));
	}

	/**
	 * 生成Panel页面对象
	 *
	 * @param entity
	 * @return
	 * @throws Exception 
	 */
	private PanelProperty createPanel(Module module,ClazzProperty entity) throws Exception{
		StringBuffer dataModelFields = new StringBuffer();
		StringBuffer columns = new StringBuffer();
		StringBuffer searchItem = new StringBuffer();
		StringBuffer dataItem = new StringBuffer();
		StringBuffer plugins = new StringBuffer();
		StringBuffer require = new StringBuffer();
		StringBuffer formModelFields = new StringBuffer();
		
		for(Property property : entity.getProperties()){
			if(property.getAnnotation().contains("@Id")){
				//主键处理
				formModelFieldsAppend(formModelFields,"id");
				
				dataItem.append("{").append(StringUtil.newLineForJS(3)).append("xtype : 'hidden',")
					.append(StringUtil.newLineForJS(3)).append("name : 'id'")
					.append(StringUtil.newLineForJS(2)).append("}");
				
				continue;
			}else if(property.getAnnotation().contains("@ManyToOne")){
				//外建处理
				
				String fkTableName = entity.getFks().get(property.getColumnName()).getFkTableName();
				ClazzProperty fkEntity = entityCreator.getEntity(fkTableName,entity.getPackageURL());
				plugins.append("'<p>");
				for(Property fkProperty : fkEntity.getProperties()){
					StringBuffer fkPropertyName = new StringBuffer();
					fkPropertyName.append(property.getName()).append(".").append(fkProperty.getName());
					
					if(fkProperty.getAnnotation().contains("@Id")){
						formModelFieldsAppend(formModelFields,fkPropertyName.toString());
					}else if(!fkProperty.getAnnotation().contains("@ManyToOne")){
						plugins.append("<b style=\"margin-left:20px;\">").append(fkProperty.getComment()).append(":</b>{").append(fkPropertyName).append("}");
						
						formModelFieldsAppend(formModelFields,fkPropertyName.toString());
					}
				}
				plugins.append("</p>',");	
				
				dataModelFieldsAppend(dataModelFields, property.getName());
				
				String fkPanel = StringUtil.merge("'JRainbow.pages.",module.getModuleName(),".",StringUtil.tableName2ClassName(fkTableName),"Panel'");
				
				require.append(",").append(fkPanel);
				formAppend(searchItem, property,true,fkPanel);
				formAppend(dataItem, property,false,fkPanel);
			}else{
				//其它字段处理
				dataModelFieldsAppend(dataModelFields, property.getName());
				columnsAppend(columns, property);
				formAppend(searchItem, property,true,null);
				formAppend(dataItem, property, false,null);
				
				formModelFieldsAppend(formModelFields, property.getName());
			}
		}

		PanelProperty panelProperty = new PanelProperty();
		panelProperty.setProjectName(entity.getProjectName());
		panelProperty.setModuleName(module.getModuleName());
		panelProperty.setColumns(columns.toString());
		panelProperty.setEntityName(entity.getClassName());
		panelProperty.setDataModelFields(dataModelFields.toString());
		panelProperty.setSearchItem(searchItem.toString());
		panelProperty.setDataItem(dataItem.toString());
		panelProperty.setPlugins(plugins.toString());
		panelProperty.setRequire(require.toString());
		panelProperty.setFormModelFields(formModelFields.toString());
		
		return panelProperty;
	}
	
	/**
	 * columns 增加数据
	 *
	 * @param columns
	 * @param property
	 */
	private void columnsAppend(StringBuffer columns,Property property){
		appendSlightPause(columns);
		columns.append("{").append(StringUtil.newLineForJS(3)).append("dataIndex : '").append(property.getName()).append("',")
			.append(StringUtil.newLineForJS(3)).append("text : '").append(property.getComment()).append("'")
			.append(StringUtil.newLineForJS(2)).append("}");

	}
	
	/**
	 * searchItem与dataItem 增加数据
	 *
	 * @param buffer
	 * @param name
	 */
	private void formAppend(StringBuffer buffer,Property property,boolean isSearch,String fkPanel){
		appendSlightPause(buffer);
		buffer.append("{").append(StringUtil.newLineForJS(3));
		
		if(fkPanel != null){
			buffer.append("xtype : 'gridfield',")
				.append(StringUtil.newLineForJS(3)).append("basePanel : Ext.create(").append(fkPanel).append(",{isTbar : false}),")
				.append(StringUtil.newLineForJS(3)).append("gridHeight : 220,")
				.append(StringUtil.newLineForJS(3)).append("valueIndex : 3,")
				.append(StringUtil.newLineForJS(3)).append("prefixName : '").append(property.getName()).append("',");
		}else{
			buffer.append("xtype : 'textfield',")
				.append(StringUtil.newLineForJS(3)).append("name : '").append(property.getName()).append("',");
		}
		
		if(isSearch){
			buffer.append(StringUtil.newLineForJS(3)).append("isSearch : true,");
		}
			
		buffer.append(StringUtil.newLineForJS(3)).append("fieldLabel : '").append(property.getComment()).append("',")
			.append(StringUtil.newLineForJS(3)).append("labelAlign : 'right'")
			.append(StringUtil.newLineForJS(2)).append("}");
	}
	
	
	/**
	 * formModelFields 增加数据
	 *
	 * @param formModelFields
	 * @param name
	 */
	private void formModelFieldsAppend(StringBuffer formModelFields,String name){
		appendSlightPause(formModelFields);
		formModelFields.append("{").append(StringUtil.newLineForJS(4))
				.append("name : '").append(name).append("'")
				.append(StringUtil.newLineForJS(3)).append("}");
	}
	
	/**
	 * dataModelFields 增加数据
	 *
	 * @param dataModelFields
	 * @param name
	 */
	private void dataModelFieldsAppend(StringBuffer dataModelFields,String name){
		appendSlightPause(dataModelFields);
		dataModelFields.append("'").append(name).append("'");
	}
	
	/**
	 * 如果不是第一次增加数据，则增加一个顿号作为分隔符
	 *
	 * @param buffer
	 */
	private void appendSlightPause(StringBuffer buffer){
		if(buffer.length() > 0){
			buffer.append(",");
		}
	}
}
